package org.arain.baseii.system.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.grid.AMGrid;
import org.arain.baseii.grid.ColumnsPojo;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.system.role.SysRoleDto;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.CommonContains;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
* <p>Title:SysRoleController </p>
* @author Arain
* @date2017年8月31日
 */
@Controller
@RequestMapping("system/user")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	
	
	@RequestMapping("list_01")
	public String list_01(Model model,HttpServletRequest request) {
		List<ColumnsPojo> columns = new ArrayList<>();
		AMGrid grid = new AMGrid("id");
		grid.setUrl("/system/user/load_grid.do");
		grid.setToolbar("UserToolbar");
		ColumnsPojo pojo;
		pojo = new ColumnsPojo("username", "用户名");
		columns.add(pojo);
		pojo = new ColumnsPojo("realname", "真实姓名");
		columns.add(pojo);
		pojo = new ColumnsPojo("email", "电子邮箱");
		columns.add(pojo);
		pojo = new ColumnsPojo("phone", "电话号码");
		columns.add(pojo);
		pojo = new ColumnsPojo("enabled", "是否可用");
		columns.add(pojo);
		grid.setColumns(columns.toString());
		
		model.addAttribute("dataGrid", grid.toString());
		return "system/user/list_01";
	}
	
	@RequestMapping(value="load_grid.do",produces="text/json;charset=UTF-8")
	@ResponseBody
	public String load_grid(QueryParam param) {
		return sysUserService.load_grid(param,CommonContains.getUserType_user());
	}
	
	@RequestMapping("add_01")
	public String add_01(SysUserDto dto,HttpServletRequest request,Model model) {
		dto.setEnabled("1");
		dto.setOrgid(AppUtils.getOrgId()+"");
		List<SysRoleDto> list = sysUserService.load_check_role(dto);
		model.addAttribute("SysUserDto", dto);
		model.addAttribute("roles", list);
		return "system/user/add_01";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ArainResult save_01(SysUserDto dto) {
		dto.setUsertype((short) 1);
		return sysUserService.save_01(dto);
	}

	@RequestMapping("edit_01")
	public String edit_01(SysUserDto dto,HttpServletRequest request,Model model) {
		dto = sysUserService.load_01(dto);
		List<SysRoleDto> list = sysUserService.load_check_role(dto);
		model.addAttribute("SysUserDto", dto);
		model.addAttribute("roles", list);
		return "system/user/add_01";
	}
	
	@RequestMapping("edit_myself")
	public String edit_myself(SysUserDto dto,HttpServletRequest request,Model model) {
		dto.setId(AppUtils.getUserId()+"");
		dto = sysUserService.load_01(dto);
		model.addAttribute("SysUserDto", dto);
		return "system/user/add_03";
	}
	
	
	@RequestMapping("save_myself.do")
	@ResponseBody
	public ArainResult save_myself(SysUserDto dto,HttpServletRequest request) {
		return sysUserService.save_myself(dto,request);
	}
	
	@RequestMapping("edit_head")
	public String edit_head(SysUserDto dto,HttpServletRequest request,Model model) {
		dto.setId(AppUtils.getUserId()+"");
		dto = sysUserService.load_01(dto);
		String image = sysUserService.load_head_image();
		if(StringUtils.isBlank(image)) {
			image="/resources/easyui/images/portrait172x172.png";
		}
		model.addAttribute("SysUserDto", dto);
		model.addAttribute("head_image", image);
		return "system/user/add_04";
	}
	
	@RequestMapping("save_head.do")
	@ResponseBody
	public ArainResult save_head(SysUserDto dto,HttpServletRequest request) {
		return sysUserService.save_head(dto,request);
	}
	
	/**
	 * 管理员
	 */
	
	@RequestMapping("list_02")
	public String list_admin(Model model,HttpServletRequest request) {
		List<ColumnsPojo> columns = new ArrayList<>();
		AMGrid grid = new AMGrid("id");
		grid.setUrl("/system/user/load_grid_admin.do");
		grid.setToolbar("UserToolbar");
		ColumnsPojo pojo;
		pojo = new ColumnsPojo("username", "用户名");
		columns.add(pojo);
		pojo = new ColumnsPojo("realname", "真实姓名");
		columns.add(pojo);
		pojo = new ColumnsPojo("email", "电子邮箱");
		columns.add(pojo);
		pojo = new ColumnsPojo("phone", "电话号码");
		columns.add(pojo);
		pojo = new ColumnsPojo("enabled", "是否可用");
		columns.add(pojo);
		grid.setColumns(columns.toString());
		
		model.addAttribute("dataGrid", grid.toString());
		return "system/user/list_02";
	}
	
	@RequestMapping(value="load_grid_admin.do",produces="text/json;charset=UTF-8")
	@ResponseBody
	public String load_grid_admin(QueryParam param) {
		return sysUserService.load_grid(param,CommonContains.getUserType_admin());
	}
	
	@RequestMapping("add_02")
	public String add_02(SysUserDto dto,HttpServletRequest request,Model model) {
		dto.setEnabled("1");
		List<SysRoleDto> list = sysUserService.load_check_role(dto);
		model.addAttribute("SysUserDto", dto);
		model.addAttribute("roles", list);
		return "system/user/add_02";
	}
	
	@RequestMapping("save_admin.do")
	@ResponseBody
	public ArainResult save_02(SysUserDto dto) {
		dto.setUsertype((short) 0);
		return sysUserService.save_01(dto);
	}

	@RequestMapping("edit_02")
	public String edit_02(SysUserDto dto,HttpServletRequest request,Model model) {
		dto = sysUserService.load_01(dto);
		List<SysRoleDto> list = sysUserService.load_check_role(dto);
		model.addAttribute("SysUserDto", dto);
		model.addAttribute("roles", list);
		return "system/user/add_02";
	}
}	
